import Vue from 'vue';
import iView from 'iview';
import VueRouter from 'vue-router';
import utils from '@/utils/index';

import routes from './routes';

Vue.use(VueRouter);

// 路由配置
const router = new VueRouter({
  //mode: 'history',
  base: __dirname,
  routes: routes
});

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  utils.title(to.meta.title);

  utils.auth.getToken().then(function(token) {
    if(!token && to.name != 'login') {
      next({name: 'login'});
    } else if(token && to.name == 'login') {
      next({name: 'homeIndex'});
    } else {
      next();
    }
  });
});

router.afterEach(() => {
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});

export default router;